package com.hmys.api.controller;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;

import cn.jsms.api.JSMSClient;
import cn.jsms.api.ValidSMSResult;
import com.hmys.api.config.SMSConfig;
import com.hmys.api.model.WebMessage;
import com.hmys.api.model.WebUser;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.service.WebMessageService;
import com.hmys.api.service.WebUserService;
import com.hmys.api.utils.ValidationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/03/28.
 */
@RestController
@Api(tags = "留言")
@RequestMapping(value = "/message")
public class MessageController {

    @Autowired
    private WebMessageService webMessageService;

    @Autowired
    private SMSConfig smsConfig;

    @Autowired
    private WebUserService webUserService;

    @ApiOperation(value = "留言", notes = "留言", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "phone", value = "手机", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "msg", value = "短信", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "msgId", value = "短信ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "type", value = "留言类型（1.购房意向、2.移民、3.留学、4.保险、5.1秒变身海外业主）", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "name", value = "姓名", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "email", value = "邮箱", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "message", value = "留言", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "location", value = "所在地", required = false, paramType = "query"),
    })
    @PostMapping(value = "/send")
    public Object login(String phone,
                        String msg,
                        String msgId, Integer type,
                        String name, String email, String message, String location) {

        if (!ValidationUtil.isPhone(phone)) {
            return new Result(ResultCode.EMPTY_EMAIL);
        }

        if (StringUtils.isEmpty(msg)) {
            return new Result(ResultCode.EMPTY_SMS_VALIDATE_CODE);
        }

        if (StringUtils.isEmpty(msgId)) {
            return new Result(ResultCode.EMPTY_MSG_ID);
        }

        if (StringUtils.isEmpty(type)) {
            return new Result(ResultCode.EMPTY_SMS_VALIDATE_CODE);
        }

        if (StringUtils.isEmpty(name)) {
            return new Result(ResultCode.EMPTY_NAME);
        }

//        if (StringUtils.isEmpty(email)) {
//            return new Result(ResultCode.EMPTY_EMAIL);
//        }
        if (!StringUtils.isEmpty(email)) {
            if (!ValidationUtil.isEmail(email)) {
                return new Result(ResultCode.ERROR_EMAIL);
            }
        }

//        if (StringUtils.isEmpty(message)) {
//            return new Result(ResultCode.EMPTY_MESSAGE);
//        }

//        if (StringUtils.isEmpty(location)) {
//            return new Result(ResultCode.EMPTY_LOCATION);
//        }

        try {
            JSMSClient jsmsClient = new JSMSClient(smsConfig.getSecret(), smsConfig.getAppkey());
            ValidSMSResult result = jsmsClient.sendValidSMSCode(msgId, msg);
            if (!result.getIsValid()) {
                return new Result(ResultCode.ERROR_VALIDATE_CODE);
            }
        } catch (APIConnectionException e) {
            return new Result(ResultCode.VALIDATE_CODE_FAILED);
        } catch (APIRequestException e) {
            return new Result(ResultCode.VALIDATE_CODE_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.ERROR);
        }

        WebMessage record = new WebMessage();
        record.setType(type);
        record.setName(name);
        record.setPhone(phone);
        record.setEmail(email);
        record.setMessage(message);
        record.setLocation(location);
        WebUser webUser = webUserService.getCurrentUser();
        if (webUser != null) {
            record.setCreateId(webUser.getUserId());
        }


        if (webMessageService.save(record) > 0) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }
}
